/*
 * OptionsCalculatorPane.java
 *
 * Created on January 13, 2008, 8:53 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.fie.contentpanes;

import com.fie.calculations.OptionsProfits;
import com.fie.datareader.HistoricalDataFetcher;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author joseacuna-rohter
 */
public class OptionsCalculatorPane {
    
    private Container totalGUI;
    
    private JTextField priceOptionField;
    private JTextField strikePriceField;
    private JTextField stockPriceField;
    private JTextField amountField;
    private JTextField tickerField;
    
    private JLabel resultsLabel;
    /** Creates a new instance of OptionsCalculatorPane */
    public OptionsCalculatorPane() {
        priceOptionField = new JTextField(10);
        strikePriceField = new JTextField(10);
        stockPriceField = new JTextField(10);
        amountField = new JTextField(10);
        tickerField = new JTextField(10);
    }
    
    
     public Container createContentPane(){
         
         totalGUI = new Container();
         totalGUI.setLayout(null);
         
         //CREATE PANEL TO SHOW TITLE
        JPanel titlePanel = new JPanel();
        titlePanel.setBackground(Color.white);
        titlePanel.setLocation(10, 10);
        titlePanel.setSize(640,40);
        JLabel titleLabel = new JLabel("Options Calculator"); 
        titleLabel.setFont(new Font("Arial Terminal",1,12));
        titlePanel.add(titleLabel);
        
        
        JPanel priceOptionPanel = new JPanel();
        priceOptionPanel.setBackground(Color.white);
        priceOptionPanel.setLocation(10, 60);
        priceOptionPanel.setSize(300,30);
        JLabel priceOptionLabel = new JLabel("Price of Option: ");
        priceOptionPanel.add(priceOptionLabel);
       // priceOptionField = new JTextField(10);        
        priceOptionPanel.add(priceOptionField);
        
        JPanel strikePricePanel = new JPanel();
        strikePricePanel.setBackground(Color.white);
        strikePricePanel.setLocation(10, 100);
        strikePricePanel.setSize(300,30);
        JLabel strikePriceLabel = new JLabel("Strike Price: ");
        strikePricePanel.add(strikePriceLabel);
        //strikePriceField = new JTextField(10);
        strikePricePanel.add(strikePriceField);
        
        JPanel stockPricePanel = new JPanel();
        stockPricePanel.setBackground(Color.white);
        stockPricePanel.setLocation(10, 140);
        stockPricePanel.setSize(300,30);
        JLabel stockPriceLabel = new JLabel("Price of Stock: ");
        stockPricePanel.add(stockPriceLabel);
       // JTextField stockPriceField = new JTextField(10);
        stockPricePanel.add(stockPriceField);
        
        JPanel amountPanel = new JPanel();
        amountPanel.setBackground(Color.white);
        amountPanel.setLocation(10, 180);
        amountPanel.setSize(300,30);
        JLabel amountLabel = new JLabel("Number of Options: ");
        amountPanel.add(amountLabel);
        //amountField = new JTextField(10);
        amountPanel.add(amountField);
        
        resultsLabel = new JLabel("0.00");
        
        
        JPanel resultsPanel = new JPanel();
        resultsPanel.setBackground(Color.white);
        resultsPanel.setLocation(10,220);
        resultsPanel.setSize(640,40);
        resultsPanel.add(resultsLabel);
        
        JPanel buttonsPanel = new JPanel();
        buttonsPanel.setBackground(Color.white);
        buttonsPanel.setLocation(10,270);
        buttonsPanel.setSize(640,40);
        
        
        JButton calculateButton = new JButton("Calculate");
        calculateButton.setBackground(Color.white);
        calculateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                actionCalculatePerformed(evt);
            }
        });
        buttonsPanel.add(calculateButton);
        
        
        
        //Right Side
        JPanel tickerPanel = new JPanel();
        tickerPanel.setBackground(Color.white);
        tickerPanel.setLocation(320, 60);
        tickerPanel.setSize(300,30);        
        JLabel tickerLabel = new JLabel("Ticker: ");
        //tickerField = new JTextField(10);
        
        JButton volatilityButton = new JButton("Calculate Volatility");
        volatilityButton.setBackground(Color.white);
        volatilityButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                actionCalculateVolPerformed(evt);
            }
        });
        
        
        tickerPanel.add(tickerLabel);
        tickerPanel.add(tickerField);
        buttonsPanel.add(volatilityButton);
        
        
        //Add everything to totalGUI
        totalGUI.add(titlePanel); 
        totalGUI.add(priceOptionPanel);
        totalGUI.add(strikePricePanel);
        totalGUI.add(stockPricePanel);
        totalGUI.add(amountPanel);
        totalGUI.add(buttonsPanel);
        totalGUI.add(resultsPanel);
        totalGUI.add(tickerPanel);
        
        return totalGUI;
         
     }
     
     
      public void actionCalculatePerformed(ActionEvent e) {
          
          System.out.println(priceOptionField.getText());
          OptionsProfits op = 
                  new OptionsProfits(Double.parseDouble(priceOptionField.getText()),
                  Double.parseDouble(strikePriceField.getText()),Double.parseDouble(stockPriceField.getText()),
                  Integer.parseInt(amountField.getText()));
          
          resultsLabel.setText("Max Profit: "+op.totalProfit() 
                + " Total Cost: " + op.totalCost());
          
          
          
      }
      
      
      public void actionCalculateVolPerformed(ActionEvent e) {
          
         try{ 
            HistoricalDataFetcher hdf = new HistoricalDataFetcher(tickerField.getText());
            resultsLabel.setText(hdf.getVolatility());
         }
         catch(Exception ex){
             ex.printStackTrace();
         }
          
      }
      
    
}
